package edu.mse.camel.client.events.peer;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;

import edu.mse.camel.client.events.AbstractEvent;
import edu.mse.camel.client.events.EventIDs;
import edu.mse.camel.client.events.IEvent;

public class ReceiveGenericPeerEvent extends AbstractEvent implements IEvent {
	
	private String specificEventId;
	private String dataType;
	private byte [] data;
	private String userID;
	private Date orginalTime;
	
	@SuppressWarnings("unchecked")
	public <T> T readObject (Class<T> entityClass ) {
		T obj = null;
		try {
			ByteArrayInputStream b = new ByteArrayInputStream(data);
			ObjectInputStream in = new ObjectInputStream(b);
			obj = (T)in.readObject();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public String getEventID() {
		if(specificEventId != null) {
			return specificEventId;
		}
		else {
			return EventIDs.RECEIVE_PEER_EVENT;
		}
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getSpecificEventId() {
		return specificEventId;
	}

	public void setSpecificEventId(String specificEventId) {
		this.specificEventId = specificEventId;
	}
	
	public String getUserID() {
		return userID;
	}
	
	public void setUserID(String userID) {
		this.userID = userID;
	}

	public void setOrginalTime(Date orginalTime) {
		this.orginalTime = orginalTime;
	}

	public Date getOrginalTime() {
		return orginalTime;
	}
}
